<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Dbmanage extends Controller {

	function Dbmanage()
	{
		parent::Controller();
                $this->load->dbutil();
                //$this->load->helper('url');
                $this->load->helper('file');
                $this->load->helper('download');
                //$this->load->helper('form');
                $this->load->helper(array('form', 'url'));
	}
	
	function index()
	{
	//代码
        $this->export();
	}
	//数据库备份
        function export(){
            // 加载数据库工具类
            $prefs = array(
                'tables'      => array(),  // 包含了需备份的表名的数组.
                'ignore'      => array(),           // 备份时需要被忽略的表
                'format'      => 'zip',             // gzip, zip, txt
                'filename'    => 'backup',    // 文件名 - 如果选择了ZIP压缩,此项就是必需的
                'add_drop'    => TRUE,              // 是否要在备份文件中添加 DROP TABLE 语句
                'add_insert'  => TRUE,              // 是否要在备份文件中添加 INSERT 语句
                'newline'     => "\n"               // 备份文件中的换行符
              );
                if (isset($_POST['backup'])){
                    if($_POST['datatype']=='alldtat'){
                        // 备份整个数据库并将其赋值给一个变量
                        $backup = $this->dbutil->backup($prefs);

                        // 加载文件辅助函数并将文件写入你的服务器

                        write_file('/files/backup.zip', $backup);

                        // 加载下载辅助函数并将文件发送到你的桌面

                        force_download('backup.zip', $backup);
                    }
             }
           $this->load->view('admin/export');
        }
        //优化数据库
        function optimize(){
                if (isset($_POST['optimize'])){
                    if($_POST['datatype']=='alldtat'){
                        // 优化整个数据库
                        $result = $this->dbutil->optimize_database();
                        if ($result !== FALSE)
                        {
                            $data['info']="数据库优化成功";
                            $data['url']=site_url('admin/dbmanage/optimize');
                            $this->load->view('admin/info',$data);
                        }
                        else
                        {
                            $data['info']="数据库优化失败，请检查数据正确性。";
                            $data['url']=site_url('admin/dbmanage/optimize');
                            $this->load->view('admin/info',$data);
                        }

                    }
             }
             else
             {
                 $this->load->view('admin/optimize');
             }
           
        }
        //恢复数据库
        function import(){
                     $this->load->view('admin/import');
        }
        //上传文件
        function do_upload()
        {
              if ($this->input->post("submit")){
                  //$classname=$this->input->post("classname");//获取班级名称
                  //$teacherid=$this->input->post("teacher");//获取班主任ID
                  $config['upload_path'] = './uploads/';//设置上传路径
                  $config['allowed_types'] = 'sql';//限制上传扩展名
                  $config['file_name'] = 'dbfile';//文件直接重命名
                  $config['overwritee'] =true;//允许覆盖上次上传的
                  $this->load->library('upload',$config);

                  if ( ! $this->upload->do_upload())
                  {
                         $error =  $this->upload->display_errors();

                         $data['info']="上传失败".$error;
                         $data['url']=site_url('admin/dbmanage/import');
                         $this->load->view('admin/info',$data);

                  }
                  else
                  {
                        include(APPPATH.'config/database.php');
                        $hostname=$db['default']['hostname'];
                        $username=$db['default']['username'];
                        $password=$db['default']['password'];
                        $database=$db['default']['database'];
                        $file="./uploads/dbfile.sql";
                        $conn=mysql_connect($hostname,$username,$password);//指定数据库连接参数 
                        mysql_select_db($database);
                        mysql_query("source '".$file."';");
                        mysql_close($conn);
                         //$data1 = array('upload_data' => $this->upload->data());
                         //$filename ="./uploads/class.csv";
                         //$this->load->model('admin','parse_csv');
                         //$this->parse_csv->parse_csv($filename,$classname,$teacherid);
                        $data['info']="导入".$file."文件到".$database."数据库完毕";
                        $data['url']=site_url('admin/manage/addclass');
                        $this->load->view('admin/info',$data);
                  }
              }


        }
}

?>